<script setup>
import { ref, computed, watch, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { clearAuth, getUser, setAutoSaveEnabled, isAutoSaveEnabled, getAutoSaveStatus, getUserInfo } from '../utils/tools/auth.js'
import { AudioEffects, setAudioLevel, getAudioLevel, getAudioSettings } from '../utils/tools/audio.js'

const router = useRouter()

// 用户信息
const user = computed(() => getUser())

// 侧边栏折叠状态
const sidebarCollapsed = ref(false)

// 当前激活的菜单项 - 根据用户角色设置默认菜单
const activeMenu = ref('')

// 搜索相关
const searchQuery = ref('')
const showSearchResults = ref(false)

// 当前时间
const currentTime = ref('')

// 通知相关
const showNotifications = ref(false)
const unreadCount = ref(3)
const notifications = ref([
  {
    id: 1,
    title: '系统更新通知',
    time: '2分钟前',
    icon: 'fas fa-info-circle',
    read: false
  },
  {
    id: 2,
    title: '新消息提醒',
    time: '5分钟前',
    icon: 'fas fa-envelope',
    read: false
  },
  {
    id: 3,
    title: '知识库同步完成',
    time: '10分钟前',
    icon: 'fas fa-check-circle',
    read: true
  }
])

// 设置相关
import SettingsModal from '../components/settings/SettingsModal.vue'

const showSettings = ref(false)
const settings = ref({
  eyeCareMode: false,
  darkMode: false,
  compactMode: false,
  autoSave: true,
  notifications: true,
  audioLevel: 0
})

// 导入导航配置
import { navigationConfig } from '../router/index.js'

// 用户角色权限映射
const userRoles = {
  1: '超级管理员',
  2: '知识库管理员', 
  3: '普通用户'
}

// 导航菜单配置
const allMenuItems = ref(navigationConfig)

// 根据用户角色过滤菜单
const menuItems = computed(() => {
  const userRole = user.value?.user_jurisdiction || 3
  return allMenuItems.value.filter(menu => menu.roles.includes(userRole))
})

// 根据用户角色设置默认菜单
const defaultMenu = computed(() => {
  const userRole = user.value?.user_jurisdiction || 3
  const filteredMenus = menuItems.value
  if (filteredMenus.length > 0) {
    return filteredMenus[0].id
  }
  return 'dashboard'
})

// 当前显示的组件 - 从路由配置中获取
const currentComponent = computed(() => {
  const currentMenu = allMenuItems.value.find(menu => menu.id === activeMenu.value)
  return currentMenu?.component || navigationConfig[0]?.component
})

// 监听用户变化，设置默认菜单
watch(() => user.value?.user_jurisdiction, (newRole) => {
  if (newRole && !activeMenu.value) {
    activeMenu.value = defaultMenu.value
  }
}, { immediate: true })

// 移除路由监听，因为我们在Home组件内部使用动态组件切换

// 当前页面内容
const currentPage = ref('dashboard')

// 方法
function logoutToLogin() {
  clearAuth()
  router.replace('/login')
}

function toggleSidebar() {
  sidebarCollapsed.value = !sidebarCollapsed.value
}

function setActiveMenu(menuId) {
  activeMenu.value = menuId
}

function handleSearch() {
  if (searchQuery.value.trim()) {
    showSearchResults.value = true
    console.log('搜索:', searchQuery.value)
    // 这里可以调用搜索API
  }
}

function handleMenuClick(menuItem) {
  if (menuItem.id) {
    setActiveMenu(menuItem.id)
    currentPage.value = menuItem.id
    console.log('切换到:', menuItem.name)
  }
}

function handleSettings() {
  showSettings.value = true
}

function closeSettings() {
  showSettings.value = false
}

function updateSetting(settingKey, value) {
  settings.value[settingKey] = value
  
  // 移除护眼模式和深色模式互斥逻辑，允许同时使用
  applySettings()
}

function resetSettings() {
  settings.value = {
    eyeCareMode: false,
    darkMode: false,
    compactMode: false,
    autoSave: true,
    notifications: true,
    audioLevel: 0
  }
  applySettings()
}

// 应用设置到DOM
function applySettings() {
  const body = document.body
  const root = document.documentElement
  
  // 先清除所有模式类和内联样式
  body.classList.remove('eye-care-mode', 'dark-mode', 'compact-mode')
  body.style.backgroundColor = ''
  body.style.color = ''
  
  // 允许护眼模式和深色模式同时使用
  if (settings.value.eyeCareMode && settings.value.darkMode) {
    // 同时开启护眼模式和深色模式
    body.classList.add('eye-care-mode', 'dark-mode')
    root.setAttribute('data-theme', 'eye-care-dark')
    // 护眼深色模式：在深色基础上应用护眼滤镜
    root.style.setProperty('--bg-primary', '#1f2937')
    root.style.setProperty('--bg-secondary', '#374151')
    root.style.setProperty('--bg-tertiary', '#4b5563')
    root.style.setProperty('--text-primary', '#f9fafb')
    root.style.setProperty('--text-secondary', '#d1d5db')
    root.style.setProperty('--text-muted', '#9ca3af')
    root.style.setProperty('--border-color', '#4b5563')
    console.log('应用护眼深色模式')
  } else if (settings.value.eyeCareMode) {
    // 仅护眼模式
    body.classList.add('eye-care-mode')
    root.setAttribute('data-theme', 'eye-care')
    // 重置为护眼模式变量
    root.style.setProperty('--bg-primary', '#fef7e6')
    root.style.setProperty('--bg-secondary', '#f3e8d3')
    root.style.setProperty('--bg-tertiary', '#e5d4b1')
    root.style.setProperty('--text-primary', '#6b4513')
    root.style.setProperty('--text-secondary', '#8b5a2b')
    root.style.setProperty('--text-muted', '#a68b5b')
    root.style.setProperty('--border-color', '#e5d4b1')
    root.style.setProperty('--accent-color', '#d4a574')
    root.style.setProperty('--accent-hover', '#c19a5c')
    console.log('应用护眼模式')
  } else if (settings.value.darkMode) {
    // 仅深色模式
    body.classList.add('dark-mode')
    root.setAttribute('data-theme', 'dark')
    // 使用CSS变量系统
    root.style.setProperty('--bg-primary', '#1f2937')
    root.style.setProperty('--bg-secondary', '#374151')
    root.style.setProperty('--bg-tertiary', '#4b5563')
    root.style.setProperty('--text-primary', '#f9fafb')
    root.style.setProperty('--text-secondary', '#d1d5db')
    root.style.setProperty('--text-muted', '#9ca3af')
    root.style.setProperty('--border-color', '#4b5563')
    console.log('应用深色模式，body类列表:', body.classList.toString())
  } else {
    // 浅色模式
    root.setAttribute('data-theme', 'light')
    // 重置为默认值
    root.style.setProperty('--bg-primary', '#ffffff')
    root.style.setProperty('--bg-secondary', '#f8fafc')
    root.style.setProperty('--bg-tertiary', '#f3f4f6')
    root.style.setProperty('--text-primary', '#1f2937')
    root.style.setProperty('--text-secondary', '#6b7280')
    root.style.setProperty('--text-muted', '#9ca3af')
    root.style.setProperty('--border-color', '#e5e7eb')
  }
  
  // 紧凑模式可以与其他模式共存
  if (settings.value.compactMode) {
    body.classList.add('compact-mode')
  }
}

function handleHelp() {
  console.log('打开帮助')
  // 这里可以打开帮助文档或帮助弹窗
}

// 组件挂载时应用设置
onMounted(() => {
  applySettings()
  
  // 添加全局测试函数
  window.testDarkMode = () => {
    settings.value.darkMode = true
    applySettings()
    console.log('测试深色模式，body类列表:', document.body.classList.toString())
    console.log('CSS变量 --bg-primary:', getComputedStyle(document.documentElement).getPropertyValue('--bg-primary'))
  }
  
  window.removeDarkMode = () => {
    settings.value.darkMode = false
    applySettings()
    console.log('移除深色模式，body类列表:', document.body.classList.toString())
  }
  
  window.testEyeCareMode = () => {
    settings.value.eyeCareMode = true
    applySettings()
    console.log('测试护眼模式，body类列表:', document.body.classList.toString())
  }
  
  window.testLightMode = () => {
    settings.value.darkMode = false
    settings.value.eyeCareMode = false
    applySettings()
    console.log('测试浅色模式，body类列表:', document.body.classList.toString())
  }
  
  window.testEyeCareDarkMode = () => {
    settings.value.darkMode = true
    settings.value.eyeCareMode = true
    applySettings()
    console.log('测试护眼深色模式，body类列表:', document.body.classList.toString())
    console.log('CSS变量 --bg-primary:', getComputedStyle(document.documentElement).getPropertyValue('--bg-primary'))
  }
  
  window.testCloseDarkMode = () => {
    // 测试关闭深色模式（护眼模式保持开启）
    settings.value.darkMode = false
    settings.value.eyeCareMode = true
    applySettings()
    console.log('测试关闭深色模式，body类列表:', document.body.classList.toString())
    console.log('CSS变量 --bg-primary:', getComputedStyle(document.documentElement).getPropertyValue('--bg-primary'))
  }
  
  window.testCloseEyeCareMode = () => {
    // 测试关闭护眼模式（深色模式保持开启）
    settings.value.darkMode = true
    settings.value.eyeCareMode = false
    applySettings()
    console.log('测试关闭护眼模式，body类列表:', document.body.classList.toString())
    console.log('CSS变量 --bg-primary:', getComputedStyle(document.documentElement).getPropertyValue('--bg-primary'))
  }
  
  window.testSettingsModal = () => {
    // 测试设置弹窗的深色模式
    showSettings.value = true
    console.log('打开设置弹窗测试深色模式')
  }
  
  window.setAutoSave = (enabled) => {
    // 设置自动保存
    setAutoSaveEnabled(enabled)
    console.log('自动保存设置:', enabled ? 'yes' : 'no')
  }
  
  window.getAutoSave = () => {
    // 获取自动保存设置
    const enabled = isAutoSaveEnabled()
    const status = getAutoSaveStatus()
    console.log('当前自动保存设置:', status)
    return enabled
  }
  
  window.getUserInfo = () => {
    // 获取完整用户信息
    const info = getUserInfo()
    console.log('用户信息:', info)
    return info
  }
  
  window.testAudio = () => {
    // 测试音效功能
    console.log('测试音效功能')
    AudioEffects.click()
  }
  
  window.testAllAudio = async () => {
    // 测试所有音效
    console.log('测试所有音效')
    const audioKeys = ['click', 'hover', 'button', 'toggle', 'notification']
    
    for (let i = 0; i < audioKeys.length; i++) {
      const key = audioKeys[i]
      console.log(`播放音效: ${key}`)
      await AudioEffects[key]()
      // 每个音效间隔500ms
      await new Promise(resolve => setTimeout(resolve, 500))
    }
  }
  
  window.setAudioLevel = (level) => {
    // 设置音效档位
    setAudioLevel(level)
    console.log('音效档位:', level === 0 ? '关闭' : `${level}档`)
  }
  
  window.getAudioSettings = () => {
    // 获取音效设置
    const settings = getAudioSettings()
    console.log('音效设置:', settings)
    return settings
  }
  
  window.testGlobalClickAudio = () => {
    // 测试全局鼠标点击音效
    console.log('测试全局鼠标点击音效')
    console.log('现在点击页面任何地方都应该有音效')
    console.log('左键和右键点击都会播放音效')
  }
  
  window.enableGlobalAudio = () => {
    // 开启全局鼠标点击音效
    setAudioLevel(2)
    console.log('已开启全局鼠标点击音效 (2档)')
    console.log('现在点击页面任何地方都会有音效')
    console.log('音效档位:', getAudioLevel())
  }
  
  
  window.forceDarkMode = () => {
    // 强制应用深色模式到所有元素
    document.documentElement.setAttribute('data-theme', 'dark')
    document.body.classList.add('dark-mode')
    
    // 强制设置CSS变量
    const root = document.documentElement
    root.style.setProperty('--bg-primary', '#1f2937')
    root.style.setProperty('--bg-secondary', '#374151')
    root.style.setProperty('--bg-tertiary', '#4b5563')
    root.style.setProperty('--text-primary', '#f9fafb')
    root.style.setProperty('--text-secondary', '#d1d5db')
    root.style.setProperty('--text-muted', '#9ca3af')
    root.style.setProperty('--border-color', '#4b5563')
    root.style.setProperty('--accent-color', '#3b82f6')
    root.style.setProperty('--accent-hover', '#2563eb')
    
    // 强制覆盖所有可能的白色背景元素
    const whiteElements = document.querySelectorAll('.container, .content, .panel, .card, .section-card, .feature-card, .stats-card, .profile-card, .activity-item, .log-item, .monitor-item')
    whiteElements.forEach(el => {
      el.style.backgroundColor = '#1f2937'
      el.style.color = '#f9fafb'
    })
    
    // 强制覆盖所有输入框和按钮
    const inputs = document.querySelectorAll('input, textarea, select, button')
    inputs.forEach(el => {
      if (el.tagName === 'INPUT' || el.tagName === 'TEXTAREA' || el.tagName === 'SELECT') {
        el.style.backgroundColor = '#4b5563'
        el.style.color = '#f9fafb'
        el.style.borderColor = '#4b5563'
      }
    })
    
    console.log('强制应用深色模式')
    console.log('CSS变量 --bg-primary:', getComputedStyle(root).getPropertyValue('--bg-primary'))
  }
})

function toggleNotifications() {
  showNotifications.value = !showNotifications.value
}

function markAllRead() {
  notifications.value.forEach(notification => {
    notification.read = true
  })
  unreadCount.value = 0
}

// 更新时间
function updateTime() {
  const now = new Date()
  currentTime.value = now.toLocaleTimeString('zh-CN', { 
    hour: '2-digit', 
    minute: '2-digit' 
  })
}

// 初始化时间并设置定时器
updateTime()
setInterval(updateTime, 1000)
</script>

<template>
  <div class="layout-container">
    <!-- 顶部导航栏 -->
    <header class="header">
      <div class="header-left">
        <button @click="toggleSidebar" class="sidebar-toggle">
          <i class="fas fa-bars"></i>
        </button>
        <div class="logo">
          <h1>KMS 知识管理系统</h1>
        </div>
      </div>
      
      <div class="header-center">
        <div class="search-box">
          <input 
            v-model="searchQuery" 
            @keyup.enter="handleSearch"
            type="text" 
            placeholder="搜索知识、文档、问答..." 
            class="search-input"
          />
          <button @click="handleSearch" class="search-btn">
            <i class="fas fa-search"></i>
          </button>
        </div>
      </div>
      
      <div class="header-right">
        <!-- 通知中心 -->
        <div class="notification-center">
          <button class="notification-btn" @click="toggleNotifications" :class="{ active: showNotifications }">
            <i class="fas fa-bell"></i>
            <span class="notification-badge" v-if="unreadCount > 0">{{ unreadCount }}</span>
          </button>
          
          <!-- 通知下拉菜单 -->
          <div class="notification-dropdown" v-if="showNotifications">
            <div class="notification-header">
              <h4>通知中心</h4>
              <button @click="markAllRead" class="mark-read-btn">全部已读</button>
            </div>
            <div class="notification-list">
              <div v-for="notification in notifications" :key="notification.id" 
                   class="notification-item" :class="{ unread: !notification.read }">
                <div class="notification-icon">
                  <i :class="notification.icon"></i>
                </div>
                <div class="notification-content">
                  <div class="notification-title">{{ notification.title }}</div>
                  <div class="notification-time">{{ notification.time }}</div>
                </div>
              </div>
              <div v-if="notifications.length === 0" class="no-notifications">
                暂无通知
              </div>
            </div>
          </div>
        </div>
      </div>
    </header>

    <div class="layout-body">
       <!-- 左侧导航栏 -->
       <aside class="sidebar" :class="{ collapsed: sidebarCollapsed }">
         <nav class="nav-menu">
           <div 
             v-for="menu in menuItems" 
             :key="menu.id"
             class="menu-item"
             :class="{ active: activeMenu === menu.id }"
             :title="sidebarCollapsed ? menu.name : ''"
             @click="handleMenuClick(menu)"
           >
             <i :class="menu.icon" class="menu-icon"></i>
             <span class="menu-name">{{ menu.name }}</span>
           </div>
         </nav>
         
         <!-- 侧边栏底部用户信息区域 -->
         <div class="sidebar-footer">
           <!-- 用户信息卡片 -->
           <div class="user-profile" :class="{ collapsed: sidebarCollapsed }">
             <div class="user-details" v-if="!sidebarCollapsed">
               <div class="user-name">{{ user?.username || '用户' }}</div>
               <div class="user-role">{{ userRoles[user?.user_jurisdiction] || '普通用户' }}</div>
             </div>
             <div class="user-actions" v-if="!sidebarCollapsed">
               <button class="user-action-btn" @click="handleSettings" title="设置">
                 <i class="fas fa-cog"></i>
               </button>
               <button class="user-action-btn logout" @click="logoutToLogin" title="退出">
                 <i class="fas fa-sign-out-alt"></i>
               </button>
             </div>
           </div>
           
           <!-- 折叠状态下的用户操作 -->
           <div class="user-actions-collapsed" v-if="sidebarCollapsed">
             <button class="user-action-btn" @click="handleSettings" title="设置">
               <i class="fas fa-cog"></i>
             </button>
             <button class="user-action-btn logout" @click="logoutToLogin" title="退出">
               <i class="fas fa-sign-out-alt"></i>
             </button>
           </div>
         </div>
       </aside>

      <!-- 主内容区域 -->
      <main class="main-content">
        <component :is="currentComponent" />
      </main>
    </div>
    
    <!-- 设置弹窗组件 -->
    <SettingsModal 
      :show="showSettings"
      :settings="settings"
      @close="closeSettings"
      @update-setting="updateSetting"
      @reset-settings="resetSettings"
    />
  </div>
</template>

<style scoped>
.layout-container {
  height: 100vh;
  display: flex;
  flex-direction: column;
  background: var(--bg-secondary);
  color: var(--text-primary);
  overflow: hidden;
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
}

/* 禁止页面滚动 */
:global(html),
:global(body) {
  overflow: hidden !important;
  height: 100vh !important;
  width: 100% !important;
  margin: 0 !important;
  padding: 0 !important;
}

:global(body) {
  position: fixed !important;
  top: 0 !important;
  left: 0 !important;
  right: 0 !important;
  bottom: 0 !important;
}

/* 顶部导航栏 */
.header {
  background: var(--bg-primary);
  border-bottom: 1px solid var(--border-color);
  color: var(--text-primary);
  padding: 0 1rem;
  height: 60px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
  position: sticky;
  top: 0;
  z-index: 100;
}

.header-left {
  display: flex;
  align-items: center;
  gap: 1rem;
  height: 100%;
}

.sidebar-toggle {
  background: none;
  border: none;
  font-size: 1.1rem;
  cursor: pointer;
  padding: 0.5rem;
  border-radius: 0.25rem;
  transition: background 0.2s, color 0.2s;
  color: #6b7280;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 2rem;
  height: 2rem;
  flex-shrink: 0;
  margin: 0;
  vertical-align: middle;
}

.sidebar-toggle:hover {
  background: #f3f4f6;
}

.sidebar-toggle i {
  display: block;
  line-height: 1;
}

.logo {
  display: flex;
  align-items: center;
  height: 100%;
  margin: 0;
  vertical-align: middle;
}

.logo h1 {
  color: var(--text-primary);
  font-size: 1.25rem;
  font-weight: 700;
  margin: 0;
  padding: 0;
  line-height: 1.2;
  display: flex;
  align-items: center;
  height: auto;
  vertical-align: middle;
}

.header-center {
  flex: 1;
  max-width: 500px;
  margin: 0 2rem;
}

.search-box {
  display: flex;
  align-items: center;
  background: var(--bg-tertiary);
  border: 1px solid var(--border-color);
  border-radius: 0.5rem;
  overflow: hidden;
}

.search-input {
  flex: 1;
  padding: 0.75rem 1rem;
  border: none;
  background: transparent;
  color: var(--text-primary);
  font-size: 0.875rem;
  outline: none;
}

.search-input::placeholder {
  color: var(--text-muted);
}

.search-btn {
  background: var(--accent-color);
  color: white;
  border: none;
  padding: 0.75rem 1rem;
  cursor: pointer;
  transition: background 0.2s;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 0.9rem;
}

.search-btn:hover {
  background: var(--accent-hover);
}

.header-right {
  display: flex;
  align-items: center;
  gap: 1rem;
  position: relative;
}

/* 通知中心样式 */
.notification-center {
  position: relative;
}

.notification-btn {
  position: relative;
  background: none;
  border: none;
  padding: 0.5rem;
  border-radius: 0.375rem;
  cursor: pointer;
  color: #6b7280;
  font-size: 1.1rem;
  transition: all 0.2s;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 2.5rem;
  height: 2.5rem;
}

.notification-btn:hover {
  background: #f3f4f6;
  color: #4f46e5;
}

.notification-btn.active {
  background: #eef2ff;
  color: #4f46e5;
}

.notification-badge {
  position: absolute;
  top: 0.25rem;
  right: 0.25rem;
  background: #ef4444;
  color: white;
  border-radius: 50%;
  width: 1.25rem;
  height: 1.25rem;
  font-size: 0.75rem;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 600;
}

.notification-dropdown {
  position: absolute;
  top: 100%;
  right: 0;
  width: 320px;
  background: var(--bg-primary);
  border: 1px solid var(--border-color);
  border-radius: 0.5rem;
  box-shadow: 0 10px 25px rgba(0, 0, 0, 0.1);
  z-index: 1000;
  margin-top: 0.5rem;
}

.notification-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 1rem;
  border-bottom: 1px solid #e5e7eb;
}

.notification-header h4 {
  margin: 0;
  font-size: 0.875rem;
  font-weight: 600;
  color: var(--text-primary);
}

.mark-read-btn {
  background: none;
  border: none;
  color: var(--accent-color);
  font-size: 0.75rem;
  cursor: pointer;
  padding: 0.25rem 0.5rem;
  border-radius: 0.25rem;
  transition: background 0.2s;
}

.mark-read-btn:hover {
  background: var(--bg-tertiary);
}

.notification-list {
  max-height: 300px;
  overflow-y: auto;
}

.notification-item {
  display: flex;
  align-items: center;
  padding: 0.75rem 1rem;
  border-bottom: 1px solid var(--border-color);
  transition: background 0.2s;
  color: var(--text-primary);
}

.notification-item:hover {
  background: var(--bg-tertiary);
}

.notification-item.unread {
  background: #fef3c7;
}

.notification-icon {
  margin-right: 0.75rem;
  color: var(--accent-color);
  font-size: 1rem;
}

.notification-content {
  flex: 1;
}

.notification-title {
  font-size: 0.875rem;
  font-weight: 500;
  color: var(--text-primary);
  margin-bottom: 0.25rem;
}

.notification-time {
  font-size: 0.75rem;
  color: var(--text-secondary);
}

.no-notifications {
  padding: 2rem 1rem;
  text-align: center;
  color: var(--text-secondary);
  font-size: 0.875rem;
}


/* 布局主体 */
.layout-body {
  display: flex;
  flex: 1;
  min-height: 0;
}

/* 左侧导航栏 */
.sidebar {
  width: 250px;
  background: var(--bg-primary);
  border-right: 1px solid var(--border-color);
  color: var(--text-primary);
  transition: width 0.3s ease;
  overflow-y: auto;
  overflow-x: hidden;
  display: flex;
  flex-direction: column;
}

.sidebar.collapsed {
  width: 60px;
}

.sidebar.collapsed .menu-name {
  opacity: 0;
  transform: translateX(-10px);
  width: 0;
  margin: 0;
}

.nav-menu {
  padding: 1rem 0;
  flex: 1;
}

.menu-group {
  margin-bottom: 0.5rem;
}

.menu-item {
  display: flex;
  align-items: center;
  padding: 0.75rem 1rem;
  cursor: pointer;
  transition: background 0.2s, padding 0.3s ease;
  color: var(--text-secondary);
  font-weight: 500;
  position: relative;
}

.sidebar.collapsed .menu-item {
  padding: 0.75rem;
  justify-content: center;
}

.menu-item:hover {
  background: var(--bg-tertiary);
}

.menu-item:hover .menu-icon {
  color: var(--accent-color);
  transform: scale(1.1);
}

.menu-item.active {
  background: var(--bg-tertiary);
  color: var(--accent-color);
  border-right: 3px solid var(--accent-color);
}

.menu-item.active .menu-icon {
  color: var(--accent-color);
}

.menu-icon {
  font-size: 1.1rem;
  margin-right: 0.75rem;
  min-width: 1.25rem;
  transition: margin 0.3s ease, color 0.2s ease;
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 1.25rem;
  height: 1.25rem;
}

.sidebar.collapsed .menu-icon {
  margin-right: 0;
}

.menu-name {
  font-size: 0.875rem;
  transition: opacity 0.3s ease, transform 0.3s ease;
  white-space: nowrap;
  overflow: hidden;
}

.submenu {
  max-height: 0;
  overflow: hidden;
  transition: max-height 0.3s ease;
  background: #f9fafb;
}

.submenu.show {
  max-height: 300px;
}

.submenu-item {
  padding: 0.5rem 1rem 0.5rem 3rem;
  cursor: pointer;
  font-size: 0.875rem;
  color: #6b7280;
  transition: background 0.2s;
}

.submenu-item:hover {
  background: #f3f4f6;
  color: #4f46e5;
}

/* 侧边栏底部区域 */
.sidebar-footer {
  border-top: 1px solid var(--border-color);
  padding: 0.75rem;
  background: var(--bg-primary);
  color: var(--text-primary);
  margin-top: auto;
}

/* 用户信息卡片 */
.user-profile {
  display: flex;
  align-items: center;
  padding: 0.75rem;
  background: transparent;
  border-radius: 0.5rem;
  gap: 0.75rem;
  transition: background 0.2s;
}

.user-profile:hover {
  background: var(--bg-tertiary);
}

.user-profile.collapsed {
  display: none; /* 完全隐藏，不占位 */
}

.user-avatar {
  font-size: 1.75rem;
  color: #4f46e5;
  flex-shrink: 0;
}

.user-profile.collapsed .user-avatar {
  font-size: 1.5rem;
}

.user-details {
  flex: 1;
  min-width: 0;
}

.user-name {
  font-weight: 600;
  color: var(--text-primary);
  font-size: 0.875rem;
  margin-bottom: 0.125rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.user-role {
  font-size: 0.75rem;
  color: var(--text-secondary);
  background: var(--bg-tertiary);
  padding: 0.125rem 0.375rem;
  border-radius: 0.25rem;
  display: inline-block;
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 100%;
}

.user-actions {
  display: flex;
  gap: 0.25rem;
  flex-shrink: 0;
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}

.user-actions-collapsed {
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
  justify-content: center;
  margin-top: 0.5rem;
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}

.user-action-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 2rem;
  height: 2rem;
  background: transparent;
  border: none;
  border-radius: 0.375rem;
  cursor: pointer;
  transition: all 0.2s;
  font-size: 0.875rem;
  color: var(--text-secondary);
}

.user-action-btn:hover {
  background: var(--bg-tertiary);
  color: var(--accent-color);
  border-color: var(--accent-color);
}

.user-action-btn.logout:hover {
  background: #fef2f2;
  color: #ef4444;
  border-color: #ef4444;
}


/* 主内容区域 */
.main-content {
  flex: 1;
  padding: 1rem;
  overflow-y: auto;
  background: var(--bg-secondary);
  color: var(--text-primary);
  height: calc(100vh - 60px);
  display: flex;
  flex-direction: column;
}

/* 内容区域样式 */
.content-wrapper {
  background: var(--bg-primary);
  color: var(--text-primary);
  border-radius: 0.75rem;
  padding: 1.5rem;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
  flex: 1;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
}

.content-wrapper h2 {
  color: var(--text-primary);
  font-size: 1.5rem;
  font-weight: 700;
  margin: 0 0 1rem 0;
  flex-shrink: 0;
}

/* 表格容器 */
.table-container {
  flex: 1;
  overflow-y: auto;
  margin-bottom: 1rem;
}

/* 分页控件 */
.pagination {
  flex-shrink: 0;
  margin-top: auto;
}

.content-wrapper p {
  color: var(--text-secondary);
  margin: 0 0 1rem 0;
}

.placeholder {
  background: #f9fafb;
  border: 2px dashed #d1d5db;
  border-radius: 0.5rem;
  padding: 3rem 2rem;
  text-align: center;
  color: #6b7280;
}

.placeholder p {
  margin: 0.5rem 0;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .sidebar {
    position: fixed;
    left: -250px;
    top: 60px;
    height: calc(100vh - 60px);
    z-index: 50;
    transition: left 0.3s ease;
  }
  
  .sidebar:not(.collapsed) {
    left: 0;
  }
  
  .header-center {
    margin: 0 1rem;
  }
  
  .main-content {
    padding: 1rem;
  }
  
  .content-wrapper {
    padding: 1rem;
  }
}

@media (max-width: 480px) {
  .header {
    padding: 0 0.5rem;
  }
  
  .header-center {
    display: none;
  }
  
  .main-content {
    padding: 0.5rem;
  }
  
  .content-wrapper {
    padding: 1rem;
  }
}

/* CSS变量定义 */
:root {
  --bg-primary: #ffffff;
  --bg-secondary: #f8fafc;
  --bg-tertiary: #f3f4f6;
  --text-primary: #1f2937;
  --text-secondary: #6b7280;
  --text-muted: #9ca3af;
  --border-color: #e5e7eb;
  --accent-color: #4f46e5;
  --accent-hover: #4338ca;
}

/* 深色模式变量 */
[data-theme="dark"] {
  --bg-primary: #1f2937;
  --bg-secondary: #374151;
  --bg-tertiary: #4b5563;
  --text-primary: #f9fafb;
  --text-secondary: #d1d5db;
  --text-muted: #9ca3af;
  --border-color: #4b5563;
  --accent-color: #3b82f6;
  --accent-hover: #2563eb;
}

/* 护眼模式变量 */
[data-theme="eye-care"] {
  --bg-primary: #fef7e6;
  --bg-secondary: #f3e8d3;
  --bg-tertiary: #e5d4b1;
  --text-primary: #6b4513;
  --text-secondary: #8b5a2b;
  --text-muted: #a68b5b;
  --border-color: #e5d4b1;
  --accent-color: #d4a574;
  --accent-hover: #c19a5c;
}

/* 全局样式 - 护眼模式、深色模式、紧凑模式 */
:global(.eye-care-mode) {
  /* 护眼模式：减少蓝光，增加暖色调 */
  background-color: var(--bg-primary) !important;
  /* 温和的整体护眼滤镜 */
  filter: sepia(0.15) hue-rotate(8deg) saturate(0.95) brightness(0.98);
  /* 保持字体清晰度 */
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-rendering: optimizeLegibility;
  /* 防止水平滚动条 */
  overflow-x: hidden;
  max-width: 100vw;
}

/* 护眼模式：主要内容区域背景 */
:global(.eye-care-mode) .main-content {
  background: #fef7e6 !important;
}

:global(.eye-care-mode) .header {
  background: #f3e8d3 !important;
  border-bottom: 1px solid #e5d4b1 !important;
}

:global(.eye-care-mode) .sidebar {
  background: #f8f0e6 !important;
  border-right: 1px solid #e5d4b1 !important;
}


:global(.eye-care-mode) .menu-item {
  color: #8b5a2b !important;
}

:global(.eye-care-mode) .menu-item:hover {
  background: #f3e8d3 !important;
  color: #6b4513 !important;
}

:global(.eye-care-mode) .menu-item.active {
  background: #e5d4b1 !important;
  color: #6b4513 !important;
}

:global(.eye-care-mode) .user-profile {
  background: #f3e8d3 !important;
  color: #8b5a2b !important;
}

:global(.eye-care-mode) .user-name {
  color: #6b4513 !important;
}

:global(.eye-care-mode) .user-role {
  background: #e5d4b1 !important;
  color: #8b5a2b !important;
}

/* 护眼模式下的文字和图标优化 */
:global(.eye-care-mode) .logo h1 {
  color: #6b4513 !important;
  font-weight: 600;
}

:global(.eye-care-mode) .menu-icon {
  color: #8b5a2b !important;
}

:global(.eye-care-mode) .menu-item.active .menu-icon {
  color: #6b4513 !important;
}

:global(.eye-care-mode) .user-action-btn {
  color: #8b5a2b !important;
}

:global(.eye-care-mode) .user-action-btn:hover {
  background: #f3e8d3 !important;
  color: #6b4513 !important;
}

/* 护眼模式下的输入框和按钮 */
:global(.eye-care-mode) input,
:global(.eye-care-mode) button,
:global(.eye-care-mode) textarea {
  background-color: #f8f0e6 !important;
  border-color: #e5d4b1 !important;
  color: #6b4513 !important;
}

:global(.eye-care-mode) input:focus,
:global(.eye-care-mode) textarea:focus {
  border-color: #d4a574 !important;
  box-shadow: 0 0 0 2px rgba(212, 165, 116, 0.2) !important;
}

/* 护眼模式布局保护 */
:global(.eye-care-mode) * {
  box-sizing: border-box;
}

:global(.eye-care-mode) .app-container {
  overflow-x: hidden;
  max-width: 100vw;
}

:global(.eye-care-mode) .header,
:global(.eye-care-mode) .sidebar,
:global(.eye-care-mode) .main-content {
  overflow-x: hidden;
  max-width: 100%;
}

:global(body.dark-mode) {
  background-color: #1f2937 !important;
  color: #f9fafb !important;
}

/* 深色模式 - 更强优先级 */
:global(body.dark-mode) * {
  color: inherit !important;
}

:global(body.dark-mode) .header {
  background: #374151 !important;
  border-bottom-color: #4b5563 !important;
}

:global(body.dark-mode) .sidebar {
  background: #374151 !important;
  border-right-color: #4b5563 !important;
}

:global(body.dark-mode) .main-content {
  background: #1f2937 !important;
}

/* 深色模式 - 强制覆盖所有背景色 */
:global(body.dark-mode) .layout-container {
  background: #1f2937 !important;
}

:global(body.dark-mode) .content-wrapper {
  background: #1f2937 !important;
  color: #f9fafb !important;
}

:global(body.dark-mode) .menu-item {
  color: #d1d5db !important;
}

:global(body.dark-mode) .menu-item:hover {
  background: #4b5563 !important;
}

:global(body.dark-mode) .menu-item.active {
  background: #1e40af !important;
  color: #dbeafe !important;
}

:global(body.dark-mode) .user-profile {
  background: transparent !important;
}

:global(body.dark-mode) .user-name {
  color: #f9fafb !important;
}

:global(body.dark-mode) .user-role {
  background: #4b5563 !important;
  color: #d1d5db !important;
}

:global(body.dark-mode) .notification-dropdown {
  background: #374151 !important;
  border-color: #4b5563 !important;
}

:global(body.dark-mode) .notification-item {
  color: #d1d5db !important;
}

:global(body.dark-mode) .notification-title {
  color: #f9fafb !important;
}

/* 深色模式 - 覆盖所有可能的背景和文字 */
:global(body.dark-mode) h1,
:global(body.dark-mode) h2,
:global(body.dark-mode) h3,
:global(body.dark-mode) h4,
:global(body.dark-mode) h5,
:global(body.dark-mode) h6 {
  color: #f9fafb !important;
}

:global(body.dark-mode) p,
:global(body.dark-mode) span,
:global(body.dark-mode) div {
  color: #d1d5db !important;
}

:global(body.dark-mode) .logo h1 {
  color: #f9fafb !important;
}

:global(body.dark-mode) .search-input {
  background: #4b5563 !important;
  color: #f9fafb !important;
  border-color: #6b7280 !important;
}

:global(body.dark-mode) .search-input::placeholder {
  color: #9ca3af !important;
}

:global(.compact-mode) .menu-item {
  padding: 0.5rem 1rem !important;
}

:global(.compact-mode) .sidebar-footer {
  padding: 0.5rem !important;
}

:global(.compact-mode) .user-profile {
  padding: 0.5rem !important;
}

:global(.compact-mode) .main-content {
  padding: 1rem !important;
}

:global(.compact-mode) .header {
  height: 50px !important;
}

:global(.compact-mode) .logo h1 {
  font-size: 1rem !important;
}
</style>


